import Global from "./store"
cc.Class({
    extends: cc.Component,

    properties: {
        P_mosterAtllas: {
            default: [],
            type: [cc.SpriteAtlas],
            tooltip: "怪物图集0小怪物1中怪物2大怪物"
        },
        P_monsterShadow: {
            default: null,
            type: cc.Node,
            tooltip: "怪物阴影"
        },
        P_monsterNode: {
            default: null,
            type: cc.Node,
            tooltip: "怪物节点"
        },
        P_monsterSprite: {
            default: [],
            type: cc.Node,
            tooltip: "怪物0左1右"
        }
    },
    ctor() {
        this.pathIndex = 0
    },
    setMonsterSpriteFrame(type, index) {
        //type 怪物类型 0小怪物1中怪物2大怪物  //index 哪一个
        this.type = type
        let frame = this.P_mosterAtllas[type].getSpriteFrame(index);
        for (let i = 0; i < this.P_monsterSprite.length; i++) {
            this.P_monsterSprite[i].getComponent(cc.Sprite).spriteFrame = frame;
        }
        this.setMonsterDirection(Global.pathSizeData[0], Global.pathSizeData[1])

    },
    setMonsterDirection(starPos, endPos) {
        if (starPos.x < endPos.x) {
            this.P_monsterSprite[0].active = false
            this.P_monsterSprite[1].active = true
        } else if (starPos.x > endPos.x) {
            this.P_monsterSprite[0].active = true
            this.P_monsterSprite[1].active = false
        }

    },
    jump() {
        //跳起落下 怪物节点
        let jumpList = [] //怪物动作列表
        let scaleUp = cc.scaleTo(Global.main.P_monsterSpeed, 0.8, 1.4);
        let scaleDown = cc.scaleTo(Global.main.P_monsterSpeed, 1, 1);
        let moveUp = cc.moveBy(Global.main.P_monsterSpeed, 0, 80)
        let moveDown = cc.moveBy(Global.main.P_monsterSpeed, 0, -80)
        let spawnUp = cc.spawn(scaleUp, moveUp) //第一个动作跳起拉高
        let spawnDown = cc.spawn(scaleDown, moveDown) //第二个动作下降正常
        jumpList.push(spawnUp)
        jumpList.push(spawnDown)
        let actionCallBack = cc.callFunc(function () {
            //第三个动作的回调方法
            if (this.type == 0) {
                let scaleForever = cc.repeatForever(cc.sequence(cc.moveTo(Global.main.P_monsterSpeed, 0, 30), cc.moveTo(Global.main.P_monsterSpeed, 0, 0))) //永跳
                let shadowScaleForever = cc.repeatForever(cc.sequence(cc.scaleTo(Global.main.P_monsterSpeed, 0.5, 0.5), cc.scaleTo(Global.main.P_monsterSpeed, 1, 1))) //永跳
                this.P_monsterNode.runAction(scaleForever)
                this.P_monsterShadow.runAction(shadowScaleForever)
            } else {
                let scaleForever = cc.repeatForever(cc.sequence(cc.scaleTo(Global.main.P_monsterSpeed, 1.2, 0.8), cc.scaleTo(Global.main.P_monsterSpeed, 0.8, 1.2))) //永缩放
                this.P_monsterNode.runAction(scaleForever)
            }
        }.bind(this))
        jumpList.push(actionCallBack)
        var jumpSequence = cc.sequence(jumpList) //生成动画队列

        var scaleSequence = cc.sequence(cc.scaleTo(Global.main.P_monsterSpeed, 0.5, 0.5), cc.scaleTo(Global.main.P_monsterSpeed, 1, 1))
        this.P_monsterNode.runAction(jumpSequence)
        this.P_monsterShadow.runAction(scaleSequence)
    },
    move() {
        //移动是整个节点
        let moveList = [] //动作列表
        for (let i = 0; i < Global.pathSizeData.length; i++) {
            let move = cc.moveTo(Global.main.P_monsterSpeed, Global.pathSizeData[i])
            let moveCallBack = cc.callFunc(() => {
                let starPos = Global.pathSizeData[this.pathIndex]
                let endPos = Global.pathSizeData[this.pathIndex + 1]
                if (Global.pathSizeData[this.pathIndex + 1] != null) {
                    this.setMonsterDirection(starPos, endPos)
                    this.pathIndex++
                }
            })
            moveList.push(move)
            moveList.push(moveCallBack)
        }
        let moveEnd = cc.callFunc(() => {
            //到最后删除自己
            this.node.removeFromParent()
        })
        moveList.push(moveEnd)
        var sequence = cc.sequence(moveList) //生成动画队列
        this.node.runAction(sequence)
    },
    action() {
        this.jump()
        this.move()
    }
});